﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
namespace Fulcrum.ViewModelGenerator
{
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics;

    #endregion Usings

    public class ViewModelEventXmlConfiguration : IViewModelEventConfiguration
    {
        #region Properties 

        public string Name { get; private set; }
        public string Type { get; private set; }
        public string EventArgsType { get; private set; }

        public string Summary { get; private set; }
        public string Remarks { get; private set; }

        #endregion Properties

        #region Ctor

        public ViewModelEventXmlConfiguration(string name, string eventArgsType, string summary)
        {
            if (String.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            if (String.IsNullOrWhiteSpace(eventArgsType))
            {
                throw new ArgumentNullException("eventArgsType");
            }

            if (String.IsNullOrWhiteSpace(summary))
            {
                throw new ArgumentNullException("summary");
            }

            this.Name = name;
            this.EventArgsType = eventArgsType;
            this.Type = this.GetDefaultEventType(this.EventArgsType);
            
            this.Summary = summary;
            this.Remarks = this.GetDefaultRemarks();
        }

        public ViewModelEventXmlConfiguration(XElement xml)
        {
            if (null == xml)
            {
                throw new ArgumentNullException("xml");
            }

            if ("Event" != xml.Name)
            {
                throw new ArgumentException("Element 'Event' expected", "xml");
            }

            this.Parse(xml);
        }

        #endregion Ctor

        #region Parse

        private void Parse(XElement xml)
        {
            Debug.Assert(null != xml && "Event" == xml.Name);

            this.Name = (string)xml.RequiredAttribute("Name");
            this.EventArgsType = (string)xml.RequiredAttribute("EventArgsType");
            this.Type = (string)xml.OptionalAttribute("Type", this.GetDefaultEventType(this.EventArgsType));

            this.Summary = (string)xml.RequiredAttribute("Summary");
            this.Remarks = (string)xml.OptionalAttribute("Remarks", this.GetDefaultRemarks());
        }

        #endregion Parse

        #region Helpers

        private string GetDefaultEventType(string eventArgsType)
        {
            Debug.Assert(false == String.IsNullOrWhiteSpace(eventArgsType));

            return eventArgsType != "EventArgs" ?
                String.Format("EventHandler<{0}>", eventArgsType) :
                "EventHandler";
        }

        private string GetDefaultRemarks()
        {
            return String.Empty;
        }

        #endregion Helpers
    }
}
